package com.audi.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.audi.R;
import com.audi.R.color;
import com.audi.dto.WifiItem;

public class HomeboxWifiSetting extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_homebox_wifi_setting);
		ActionBar mActionBar = getActionBar();
		mActionBar.setTitle("Wifi connection");
		mActionBar.setBackgroundDrawable(new ColorDrawable(
				color.background_action_bar));
		mActionBar.setDisplayHomeAsUpEnabled(true);
		mActionBar.setDisplayShowHomeEnabled(false);

		ListView listView = (ListView) findViewById(R.id.list_wifi);
		final ArrayList<WifiItem> alist = new ArrayList<WifiItem>();

		for (int i = 0; i < 3; i++) {
			WifiItem item = new WifiItem();
			item.setWifiName("Wifi " + i);
			alist.add(item);
		}

		WifiAdapter adapt = new WifiAdapter(this, alist);
		listView.setAdapter(adapt);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent resultIntent = new Intent();
				resultIntent.putExtra("wifi", alist.get(arg2).getWifiName());
				setResult(RESULT_OK, resultIntent);
				finish();
			}

		});

	}

	public class WifiAdapter extends BaseAdapter {
		Activity activity;
		LayoutInflater inflater;
		private ArrayList<WifiItem> mList;

		public WifiAdapter(Activity arg1, ArrayList<WifiItem> list) {
			activity = arg1;
			mList = list;
			inflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@SuppressLint({ "ViewHolder", "InflateParams" })
		public View getView(int pos, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.item_wifi_setting, null);
			TextView txt_wifi_name = (TextView) convertView
					.findViewById(R.id.wifi_name);
			TextView txt_wifi_sts = (TextView) convertView
					.findViewById(R.id.wifi_status_connection);
			txt_wifi_name.setText(mList.get(pos).getWifiName());
			txt_wifi_sts.setText("connect");
			return convertView;
		}

	}

}
